package pers.acp.test.application.init

import com.mybatisflex.core.dialect.DbTypeUtil
import com.mybatisflex.core.query.QueryWrapper
import io.gitee.zhangbinhub.acp.boot.init.AcpInitialization
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import org.noear.solon.annotation.Component
import org.noear.solon.annotation.Inject
import org.noear.solon.core.util.ResourceUtil
import org.noear.solon.data.annotation.Transaction
import org.noear.solon.data.sql.SqlUtils
import pers.acp.test.application.entity.TableOne
import pers.acp.test.application.entity.TableTwo
import pers.acp.test.application.mapper.TableOneMapper
import pers.acp.test.application.mapper.TableTwoMapper
import java.math.BigDecimal
import javax.sql.DataSource

@Component
open class DataInitialization(
    private val logAdapter: LogAdapter,
    @Inject("db1") private val sqlUtils1: SqlUtils,
    @Inject("db2") private val sqlUtils2: SqlUtils,
    @Inject("db1") private val dataSource1: DataSource,
    @Inject("db2") private val dataSource2: DataSource,
    @Inject private val tableOneMapper: TableOneMapper,
    @Inject private val tableTwoMapper: TableTwoMapper
) : AcpInitialization {
    override val name: String
        get() = "data initialization"

    @Transaction
    override fun doInit() {
        initTable()
        initData()
        logAdapter.debug("test debug log!!!!!")
    }

    private fun initTable() {
        ResourceUtil.getResourceAsString(
            "sql/${
                DbTypeUtil.getDbType(dataSource1).getName()
            }/schema1.sql"
        )?.apply {
            for (sql in this.split(";")) {
                if (sql.trim { it <= ' ' }.length > 10) {
                    sqlUtils1.sql(sql).update()
                }
            }
        }
        ResourceUtil.getResourceAsString(
            "sql/${
                DbTypeUtil.getDbType(dataSource2).getName()
            }/schema2.sql"
        )?.apply {
            for (sql in this.split(";")) {
                if (sql.trim { it <= ' ' }.length > 10) {
                    sqlUtils2.sql(sql).update()
                }
            }
        }
    }

    private fun initData() {
        if (tableOneMapper.selectCountByQuery(QueryWrapper.create()) == 0L) {
            mutableListOf<TableOne>().apply {
                for (i in 1..10) {
                    this.add(TableOne().apply {
                        this.name = "name$i"
                        this.valueValue = BigDecimal(1000 + i)
                    })

                }
            }.apply {
                tableOneMapper.insertBatch(this)
            }.apply {
                println("init data to table11:")
                println(this)
            }
        }
        if (tableTwoMapper.selectCountByQuery(QueryWrapper.create()) == 0L) {
            mutableListOf<TableTwo>().apply {
                for (i in 1..10) {
                    this.add(TableTwo().apply {
                        this.name = "name$i"
                        this.value = BigDecimal(1000 + i)
                    })
                }
            }.apply {
                tableTwoMapper.insertBatch(this)
            }
        }
//        throw WebException("test exception")
    }
}